#include <stdio.h>
#include <string.h>
#include <stdlib.h>
-#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <sys/select.h>
libxl_device_nic *vifs, int num_vifs,
libxl_device_model_starting **starting_r)
{
- char *path, *logfile, *logfile_new;
- struct stat stat_buf;
+ char *path, *logfile;
int logfile_w, null;
- int i, rc;
+ int rc;
char **args;
struct libxl_spawn_starting buf_spawn, *for_spawn;
path = libxl_sprintf(ctx, "/local/domain/0/device-model/%d", info->domid);
xs_mkdir(ctx->xsh, XBT_NULL, path);
- logfile = libxl_sprintf(ctx, "/var/log/xen/qemu-dm-%s.log", info->dom_name);
- if (stat(logfile, &stat_buf) == 0) {
- /* file exists, rotate */
- logfile = libxl_sprintf(ctx, "/var/log/xen/qemu-dm-%s.log.10", info->dom_name);
- unlink(logfile);
- for (i = 9; i > 0; i--) {
- logfile = libxl_sprintf(ctx, "/var/log/xen/qemu-dm-%s.log.%d", info->dom_name, i);
- logfile_new = libxl_sprintf(ctx, "/var/log/xen/qemu-dm-%s.log.%d", info->dom_name, i + 1);
- rename(logfile, logfile_new);
- }
- logfile = libxl_sprintf(ctx, "/var/log/xen/qemu-dm-%s.log", info->dom_name);
- logfile_new = libxl_sprintf(ctx, "/var/log/xen/qemu-dm-%s.log.1", info->dom_name);
- rename(logfile, logfile_new);
- }
- logfile = libxl_sprintf(ctx, "/var/log/xen/qemu-dm-%s.log", info->dom_name);
+ libxl_create_logfile(ctx, libxl_sprintf(ctx, "qemu-dm-%s", info->dom_name), &logfile);
logfile_w = open(logfile, O_WRONLY|O_CREAT, 0644);
+ free(logfile);
null = open("/dev/null", O_RDONLY);
if (starting_r) {
#include "xen_uuid.h"
+#define XL_LOGGING_ENABLED
+
typedef void (*libxl_log_callback)(void *userdata, int loglevel, const char *file,
int line, const char *func, char *s);
-
struct libxl_dominfo {
xen_uuid_t uuid[16];
uint32_t domid;
#define ERROR_NOMEM (-1032)
#define ERROR_INVAL (-1245)
+/* logging */
+void xl_logv(struct libxl_ctx *ctx, int errnoval, int loglevel, const char *file, int line, const char *func, char *fmt, va_list al);
+void xl_log(struct libxl_ctx *ctx, int errnoval, int loglevel, const char *file, int line, const char *func, char *fmt, ...);
+
+#ifdef XL_LOGGING_ENABLED
+#define XL_LOG(ctx, loglevel, _f, _a...) xl_log(ctx, loglevel, -1, __FILE__, __LINE__, __func__, _f, ##_a)
+#define XL_LOG_ERRNO(ctx, loglevel, _f, _a...) xl_log(ctx, loglevel, errno, __FILE__, __LINE__, __func__, _f, ##_a)
+#define XL_LOG_ERRNOVAL(ctx, errnoval, loglevel, _f, _a...) xl_log(ctx, loglevel, errnoval, __FILE__, __LINE__, __func__, _f, ##_a)
+#else
+#define XL_LOG(ctx, loglevel, _f, _a...)
+#define XL_LOG_ERRNO(ctx, loglevel, _f, _a...)
+#define XL_LOG_ERRNOVAL(ctx, loglevel, errnoval, _f, _a...)
+#endif
+
+#define XL_LOG_DEBUG 3
+#define XL_LOG_INFO 2
+#define XL_LOG_WARNING 1
+#define XL_LOG_ERROR 0
+
/* context functions */
int libxl_ctx_init(struct libxl_ctx *ctx);
int libxl_ctx_free(struct libxl_ctx *ctx);
#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
-
-#define XL_LOGGING_ENABLED
-
-#ifdef XL_LOGGING_ENABLED
-#define XL_LOG(ctx, loglevel, _f, _a...) xl_log(ctx, loglevel, -1, __FILE__, __LINE__, __func__, _f, ##_a)
-#define XL_LOG_ERRNO(ctx, loglevel, _f, _a...) xl_log(ctx, loglevel, errno, __FILE__, __LINE__, __func__, _f, ##_a)
-#define XL_LOG_ERRNOVAL(ctx, errnoval, loglevel, _f, _a...) xl_log(ctx, loglevel, errnoval, __FILE__, __LINE__, __func__, _f, ##_a)
-#else
-#define XL_LOG(ctx, loglevel, _f, _a...)
-#define XL_LOG_ERRNO(ctx, loglevel, _f, _a...)
-#define XL_LOG_ERRNOVAL(ctx, loglevel, errnoval, _f, _a...)
-#endif
-
-#define XL_LOG_DEBUG 3
-#define XL_LOG_INFO 2
-#define XL_LOG_WARNING 1
-#define XL_LOG_ERROR 0
-
-void xl_logv(struct libxl_ctx *ctx, int errnoval, int loglevel, const char *file, int line, const char *func, char *fmt, va_list al);
-void xl_log(struct libxl_ctx *ctx, int errnoval, int loglevel, const char *file, int line, const char *func, char *fmt, ...);
-
typedef enum {
DEVICE_VIF,
DEVICE_VBD,
#include <xenctrl.h>
#include <ctype.h>
#include <errno.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <unistd.h>
#include "libxl_utils.h"
#include "libxl_internal.h"
return 0;
}
+int libxl_create_logfile(struct libxl_ctx *ctx, char *name, char **full_name)
+{
+ struct stat stat_buf;
+ char *logfile, *logfile_new;
+ int i;
+
+ logfile = libxl_sprintf(ctx, "/var/log/xen/%s.log", name);
+ if (stat(logfile, &stat_buf) == 0) {
+ /* file exists, rotate */
+ logfile = libxl_sprintf(ctx, "/var/log/xen/%s.log.10", name);
+ unlink(logfile);
+ for (i = 9; i > 0; i--) {
+ logfile = libxl_sprintf(ctx, "/var/log/xen/%s.log.%d", name, i);
+ logfile_new = libxl_sprintf(ctx, "/var/log/xen/%s.log.%d", name, i + 1);
+ rename(logfile, logfile_new);
+ }
+ logfile = libxl_sprintf(ctx, "/var/log/xen/%s.log", name);
+ logfile_new = libxl_sprintf(ctx, "/var/log/xen/%s.log.1", name);
+ rename(logfile, logfile_new);
+ }
+ *full_name = strdup(logfile);
+ return 0;
+}
+
int libxl_param_to_domid(struct libxl_ctx *ctx, char *p, uint32_t *domid);
int libxl_get_stubdom_id(struct libxl_ctx *ctx, int guest_domid);
int libxl_is_stubdom(struct libxl_ctx *ctx, int domid);
+int libxl_create_logfile(struct libxl_ctx *ctx, char *name, char **full_name);
#endif
#include "libxl.h"
#include "libxl_utils.h"
+int logfile = 2;
+
void log_callback(void *userdata, int loglevel, const char *file, int line, const char *func, char *s)
{
- fprintf(stderr, "[%d] %s:%d:%s: %s\n", loglevel, file, line, func, s);
+ char str[1024];
+
+ snprintf(str, sizeof(str), "[%d] %s:%d:%s: %s\n", loglevel, file, line, func, s);
+ write(logfile, str, strlen(str));
}
static void printf_info(libxl_domain_create_info *c_info,
libxl_domain_unpause(&ctx, domid);
if (need_daemon) {
+ char *fullname, *name;
+
+ asprintf(&name, "xl-%s", info1.name);
+ libxl_create_logfile(&ctx, name, &fullname);
+ logfile = open(fullname, O_WRONLY|O_CREAT, 0644);
+ free(fullname);
+ free(name);
+
daemon(0, 0);
need_daemon = 0;
}
+ XL_LOG(&ctx, XL_LOG_DEBUG, "Waiting for domain %s (domid %d) to die", info1.name, domid);
libxl_wait_for_domain_death(&ctx, domid, &fd);
while (1) {
if (!ret)
continue;
if (libxl_is_domain_dead(&ctx, domid, &info)) {
+ XL_LOG(&ctx, XL_LOG_DEBUG, "Domain %d is dead", domid);
if (info.crashed || info.dying || (info.shutdown && (info.shutdown_reason != SHUTDOWN_suspend))) {
+ XL_LOG(&ctx, XL_LOG_DEBUG, "Domain %d needs to be clean: destroying the domain", domid);
libxl_domain_destroy(&ctx, domid, 0);
if (info.shutdown && (info.shutdown_reason == SHUTDOWN_reboot)) {
libxl_ctx_free(&ctx);
+ XL_LOG(&ctx, XL_LOG_DEBUG, "Done. Rebooting now");
goto start;
}
+ XL_LOG(&ctx, XL_LOG_DEBUG, "Done. Exiting now");
}
+ XL_LOG(&ctx, XL_LOG_DEBUG, "Domain %d does not need to be clean, exiting now", domid);
exit(0);
}
}
+ close(logfile);
for (i = 0; i < num_vifs; i++) {
free(vifs[i].smac);
free(vifs[i].ifname);